<?php

namespace Trawind\Events\Subscribers\Inventory\Receipt;

use Trawind\Events\Events\Inventory\Fulfillment\ClosePickEvent;
use Trawind\Events\Events\Inventory\Fulfillment\DeletePickEvent;
use Trawind\Events\Events\Inventory\Quantity\DeleteQuantityControlEvent;
use Trawind\Events\Events\Inventory\Quantity\StoreQuantityControlEvent;
use Trawind\Events\Events\Inventory\Quantity\SubmitQuantityControlEvent;
use Trawind\Events\Events\Inventory\Receipt\DeletePutawayEvent;
use Trawind\Events\Events\Inventory\Receipt\DeleteReceiptEvent;
use Trawind\Events\Events\Inventory\Receipt\StoreNoticeRecordEvent;
use Trawind\Events\Events\Inventory\Receipt\StorePutawayEvent;
use Trawind\Events\Events\Purchase\ClosePurchaseEvent;
use Trawind\Extension\Models\Deploy\BusinessType;

class ReceiptNoticeSubscriber
{
    private $businessType = \Trawind\Basics\Enums\BusinessType::INVENTORY_RECEIPT_NOTICE;

    public function storePutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StorePutawayEvent::class);
        }
    }

    public function deletePutaway($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeletePutawayEvent::class);
        }
    }

    public function closePurchase($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, ClosePurchaseEvent::class);
        }
    }

    public function storeNoticeRecord($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StoreNoticeRecordEvent::class);
        }
    }

    public function deleteReceipt($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeleteReceiptEvent::class);
        }
    }

    public function storeQuantityControl($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, StoreQuantityControlEvent::class);
        }
    }

    public function submitQuantityControl($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, SubmitQuantityControlEvent::class);
        }
    }

    public function deleteQuantityControl($event)
    {
        if ($this->businessType == $event->payload['business_type_id']) {
            $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload, DeleteQuantityControlEvent::class);
        }
    }

    /**
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            StorePutawayEvent::class,
            static::class . '@storePutaway'
        );

        $events->listen(
            DeletePutawayEvent::class,
            static::class . '@deletePutaway'
        );

        $events->listen(
            ClosePurchaseEvent::class,
            static::class . '@closePurchase'
        );

        $events->listen(
            StoreNoticeRecordEvent::class,
            static::class . '@storeNoticeRecord'
        );

        $events->listen(
            DeleteReceiptEvent::class,
            static::class . '@deleteReceipt'
        );

        $events->listen(
            StoreQuantityControlEvent::class,
            static::class . '@storeQuantityControl'
        );

        $events->listen(
            SubmitQuantityControlEvent::class,
            static::class . '@submitQuantityControl'
        );

        $events->listen(
            DeleteQuantityControlEvent::class,
            static::class . '@deleteQuantityControl'
        );
    }
}
